package cn.ecut.service.impl;

import checkers.igj.quals.I;
import cn.ecut.dao.ScholarshipApplyDao;
import cn.ecut.model.Examine;
import cn.ecut.model.Relative;
import cn.ecut.model.ScholarshipApply;
import cn.ecut.model.Student;
import cn.ecut.service.ScholarshipApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@Service
public class ScholarshipApplyServiceimpl implements ScholarshipApplyService {
    @Autowired
    private ScholarshipApplyDao scholarshipApplyDao;
    private Integer MaxApplyNum=3;

    @Override
    public String selectArea(String id) {
        return scholarshipApplyDao.selectArea(id);
    }

    @Override
    public Integer insertScholarshipApply(ScholarshipApply scholarshipApply) {
        return scholarshipApplyDao.insertScholarshipApply(scholarshipApply);
    }

    @Override
    public List<Relative> TransRelative(String relationShip, String relationName, String relationOccupation, String relationPhone) {
        String s1 = relationShip.replaceAll("\\\"","");
        String t1 = s1.replace("[", "").replace("]","");
        String s2 = relationName.replaceAll("\\\"","");
        String t2 = s2.replace("[", "").replace("]","");
        String s3 = relationOccupation.replaceAll("\\\"","");
        String t3 = s3.replace("[", "").replace("]","");
        String s4 = relationPhone.replaceAll("\\\"","");
        String t4 = s4.replace("[", "").replace("]","");
        String[] strShip= t1.split(",");
        String[] strName= t2.split(",");
        String[] strOccupation= t3.split(",");
        String[] strPhone= t4.split(",");
        List<Relative> list = new ArrayList<>();
        for (int i=0;i<strPhone.length;i++){
            Relative relative = new Relative();
            relative.setRelationShip(strShip[i]);
            relative.setRelationName(strName[i]);
            relative.setRelationOccupation(strOccupation[i]);
            relative.setRelationPhone(strPhone[i]);
            list.add(relative);
        }
        return list;
    }

    @Override
    public String jsonTrans(String s) {
        String s1 = s.replaceAll("\\\"","");
        String t1 = s1.replace("[", "").replace("]","");
        return t1;
    }

    @Override
    public Integer insertRelative(List<Relative> relativeList) {
        return scholarshipApplyDao.insertRelative(relativeList);
    }

    @Override
    public void updataApplyNum(Integer sid) {
        scholarshipApplyDao.updataApplyNum(sid);
    }

    @Override
    public Integer selectScholarshipResidue(Integer sid) {
        return scholarshipApplyDao.selectScholarshipResidue(sid);
    }

    @Override
    public Integer selectApplyCount(String number) {
        return selectApplyCount(number);
    }

    @Override
    public boolean selectIsApply(String number,Integer sid) {
        List<Integer> list =scholarshipApplyDao.selectIsApply(number);
        for (int i=0;i<list.size();i++){
            if(sid==list.get(i)){
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean IsOutCount(String number) {
        if(scholarshipApplyDao.selectApplyCount(number)>=MaxApplyNum){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean selectIsHave(String number) {
        List<Integer> list =scholarshipApplyDao.selectIsHave(number);
        System.out.println(list);
        boolean t=true;
        for (int i=0;i<list.size();i++){
            System.out.print("list:");
            System.out.println(list.get(i));
            if(list.get(i)==1){
                t=false;
                System.out.print("t:");
                System.out.println(t);
            }
        }
        return t;
    }

    @Override
    public List<Examine> selectExamineByPage(int offset, int num) {
        return scholarshipApplyDao.selectExamineByPage(offset, num);
    }

    @Override
    public Integer selectAllApplyCount() {
        return scholarshipApplyDao.selectAllApplyCount();
    }

    @Override
    public List<Examine> advanceSelect(String s, int offset, int num) {
        return scholarshipApplyDao.advanceSelect(s, offset, num);
    }

    @Override
    public Integer advanceSelectCount(String s) {
        return scholarshipApplyDao.advanceSelectCount(s);
    }

    @Override
    public int selectScholarshipApplyStateByid(Integer id) {
        return scholarshipApplyDao.selectScholarshipApplyStateByid(id);
    }

    @Override
    public Integer deleteScholarshipApplyByid(Integer id) {
        return scholarshipApplyDao.deleteScholarshipApplyByid(id);
    }

    @Override
    public Student selectStudentAndRelative(String number, Integer sid) {
        return scholarshipApplyDao.selectStudentAndRelative(number, sid);
    }


    @Override
    public ScholarshipApply selectScholarship(Integer id) {
        return scholarshipApplyDao.selectScholarship(id);
    }

    @Override
    public Integer selectSidByid(Integer id) {
        return scholarshipApplyDao.selectSidByid(id);
    }

    @Override
    public Integer pass(Integer id) {
        return scholarshipApplyDao.pass(id);
    }

    @Override
    public Integer out(Integer id) {
        return scholarshipApplyDao.out(id);
    }
}
